Manim Community 学习笔记

Posted by 3nit on 2024-01-26

Catalog


Python

(还不太会)
https://www.runoob.com/python3/python3-basic-syntax.html

  • 推导式
  • 迭代器、生成器
  • 函数传参
  • 数据结构
  • 模块、包
  • 异常

Manim

Mobject

Vectorized Mobject

vector graphics

Mobject 显示

原点为屏幕中心

add(), remove()
shift(), move_to(), next_to(), align_to()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from manim import *

class MobjectPlacement(Scene):
def construct(self):
circle = Circle()
square = Square()
triangle = Triangle()

# place the circle two units left from the origin
circle.move_to(LEFT * 2)
# place the square to the left of the circle
square.next_to(circle, LEFT)
# align the left border of the triangle to the left border of the circle
triangle.align_to(circle, LEFT)

self.add(circle, square, triangle)
self.wait(1)

The move_to() method uses absolute units (measured relative to the ORIGIN),
while next_to() uses relative units (measured from the mobject passed as the first argument).
align_to() uses LEFT not as measuring units but as a way to determine the border to use for alignment. The coordinates of the borders of a mobject are determined using an imaginary bounding box around it.

Many methods in manim can be chained together. Technically, this is possible because most methods calls return the modified mobject.
right_square = Square(color=GREEN, fill_opacity=0.7).shift(2 * RIGHT).shift(UP)

Mobject 风格

1
2
3
4
5
circle.set_stroke(color=GREEN, width=20)
square.set_fill(YELLOW, opacity=1.0)
triangle.set_fill(PINK, opacity=0.5)

right_square = Square(color=GREEN, fill_opacity=0.7)

Only instances of VMobject implement set_stroke() and set_fill(). Instances of Mobject implement set_color() instead. The vast majority of pre-defined classes are derived from VMobject so it is usually safe to assume that you have access to set_stroke() and set_fill().

Mobject 顺序

1
2
self.add(triangle, square, circle)
#靠前的参数在下层

动画

动画类
1
2
3
4
5
6
7
8
# some animations display mobjects, ...
self.play(FadeIn(square))

# ... some move or rotate mobjects around...
self.play(Rotate(square, PI/4))

# some animations remove mobjects from the screen
self.play(FadeOut(square))
.animate 函数
1
2
3
4
5
6
# animate the change of color
self.play(square.animate.set_fill(WHITE))
self.wait(1)

# animate the change of position and the rotation at the same time
self.play(square.animate.shift(UP).rotate(PI / 3))

animate() is a property of all mobjects that animates the methods that come afterward.

动画持续时间
1
self.play(square.animate.shift(UP), run_time=3)

By default, any animation passed to play() lasts for exactly one second.

自定义动画